package com.hzj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzj.mapper.VoteItemMapper;
import com.hzj.mapper.VoteOptionMapper;
import com.hzj.pojo.VoteOption;
import com.hzj.pojo.VoteSubject;
import com.hzj.mapper.VoteSubjectMapper;
import com.hzj.service.VoteOptionService;
import com.hzj.service.VoteSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzj
 * @since 2021-03-19
 */
@Service
public class VoteSubjectServiceImpl extends ServiceImpl<VoteSubjectMapper, VoteSubject> implements VoteSubjectService {
    @Autowired
    VoteSubjectMapper vsm;
    @Autowired
    VoteItemMapper vim;
    @Autowired
    VoteOptionService vos;
    @Autowired
    VoteOptionMapper vom;

    @Override
    public Page showAll(int pageIndex, int paheSize, String title) {
        Page page = new Page(pageIndex, paheSize);
        QueryWrapper wrapper = null;
        Page selectPage = null;
        if (!"".equals(title) || title != null) {
            wrapper = new QueryWrapper();
            wrapper.eq("vs_title", title);
            selectPage = vsm.selectPage(page, wrapper);
        } else {
            selectPage = vsm.selectPage(page, null);
        }


        List<VoteSubject> subjectList = selectPage.getRecords();
        for (int i = 0; i < subjectList.size(); i++) {
            VoteSubject subject = subjectList.get(i);
            subject.setVoteOptions(vom.countVo(subject.getVsId()));
            subject.setVoteItems(vim.countVi(subject.getVsId()));
        }
        selectPage.setRecords(subjectList);
        return selectPage;
    }

    @Override
    public IPage<Map<String, Object>> showCount(Page page, String title) {
        return vsm.showCount(page, title);
    }

    @Override
    @Transactional
    public boolean addSubjectAndOption(VoteSubject voteSubject, List<VoteOption> list) {
        try {
            int insert = vsm.insert(voteSubject);
//            System.out.println("插入结果===========>"+(list.size() != insertatch));
            if (insert < 1) {
                throw new Exception("插入VoteSubject对象出错");
            } else {
                int insertatch = vos.insertatch(list);

                if (list.size() != insertatch) {
                    throw new Exception("插入VoteOption对象出错");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Map<String, Object> checkSubject(int id) {
        return vsm.checkSubject(id);
    }
}
